package cn.ecut.dzy.atomics;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @program: juc
 * @description:
 * @author: DingZhenYun
 * @create: 2021-10-26 22:29
 **/

public class AtomicMarkableReferenceDemo {
    static AtomicInteger atomicInteger = new AtomicInteger(100);
    static AtomicStampedReference<Integer> stampedReference = new AtomicStampedReference<>(100,1);
    static  AtomicMarkableReference<Integer> atomicMarkableReferenceDemo=new AtomicMarkableReference(100,false);
    public static void main(String[] args) {
        new Thread(() -> {
            boolean marked = atomicMarkableReferenceDemo.isMarked();
            System.out.println(Thread.currentThread().getName()+"\t"+"---默认修改标识："+marked);
            try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
            atomicMarkableReferenceDemo.compareAndSet(100,101,marked,!marked);
        },"t1").start();

        new Thread(() -> {
            boolean marked = atomicMarkableReferenceDemo.isMarked();
            System.out.println(Thread.currentThread().getName()+"\t"+"---默认修改标识："+marked);
            try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
            boolean b = atomicMarkableReferenceDemo.compareAndSet(100, 20210308, marked, !marked);

            System.out.println(Thread.currentThread().getName()+"\t"+"---操作是否成功:"+b);
            System.out.println(Thread.currentThread().getName()+"\t"+atomicMarkableReferenceDemo.getReference());
            System.out.println(Thread.currentThread().getName()+"\t"+atomicMarkableReferenceDemo.isMarked());

        },"t2").start();

    }
}
